package com.cy.service.impl;


import com.cy.common.ResultResponse;
import com.cy.entity.UserCountByLastname;
import com.cy.entity.Users;
import com.cy.entity.vo.UserVo;
import com.cy.mapper.UsersMapper;
import com.cy.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aleven
 * @since 2024-12-21
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;


    @Override
    public ResultResponse UserLogin(UserVo userVo) throws Exception {
        if (userVo.getInfo() == null||userVo.getPassword()==null) {
            return ResultResponse.error();
        }
        Users user = usersMapper.findUser(userVo.getInfo());
        if (user == null) {
            return ResultResponse.notFound();
        }
        if (user.getPassword().equals(userVo.getPassword())){
            Map<String,Object> map = new HashMap<>();
            try {
                Map<String, String> payload = new HashMap<>();
                payload.put("userId", String.valueOf(user.getUserID()));
                payload.put("username",user.getUsername());
                payload.put("roleID", String.valueOf(user.getRoleID()));
                // 生成jwt令牌
                String token = JWTUtils.getToken(payload);
                map.put("state",true);
                map.put("msg","认证成功！");
                map.put("token",token);  // 响应token
                return ResultResponse.success(token);
            } catch (Exception e) {
                map.put("state",false);
                map.put("msg",e.getMessage());
            }

        }
        return ResultResponse.pwderror();
    }
    @Override
    public ResultResponse UserRegister(Users users) throws Exception {
        if (users.getUsername() == null||users.getPassword()==null||users.getEmail()== null||users.getPhoneNumber()== null|| users.getRoleID()==null) {
            return ResultResponse.error();
        }
        Users user = usersMapper.findUser(users.getEmail());
        if (user != null) {
            return ResultResponse.exist();
        }
        int count = usersMapper.insertUser(users);
        Map<String,Object> map = new HashMap<>();
        try {
            Map<String, String> payload = new HashMap<>();
            payload.put("userId", String.valueOf(user.getUserID()));
            payload.put("username",user.getUsername());
            payload.put("roleID", String.valueOf(user.getRoleID()));
            // 生成jwt令牌
            String token = JWTUtils.getToken(payload);
            map.put("state",true);
            map.put("msg","认证成功！");
            map.put("token",token);  // 响应token
            return ResultResponse.success(token);
        } catch (Exception e) {
            map.put("state",false);
            map.put("msg",e.getMessage());
        }
        return count>0?ResultResponse.success():ResultResponse.fail();
    }

    @Override
    public ResultResponse forgetPwd(UserVo userVo) {
        if (userVo.getInfo() == null||userVo.getPassword()==" ") {
            return ResultResponse.error();
        }
        Users user = usersMapper.findUser(userVo.getInfo());
        if (user == null) {
            return ResultResponse.notFound();
        }
        user.setPassword(userVo.getPassword());//更新密码
        int count = usersMapper.updatePassword(user);

        return count>0?ResultResponse.success():ResultResponse.fail();
    }


    @Override
    public List<Users> findusersAll() {
        return usersMapper.findusersAll();
    }
    @Override
    public Users findusersById(String UserID) {
        return usersMapper.findusersById(UserID);
    }
    @Override
    public Integer addUsers(Users user) {
        return usersMapper.addUsers(user);
    }
    @Override
    public Integer updateUsers(Users user) {
        return usersMapper.updateUsers(user);
    }
    @Override
    public Integer deleteUsers(Integer UserId) {
        return usersMapper.deleteUsers(UserId);
    }
    @Override
    public Integer countUsers() {
        return usersMapper.countUsers();
    }
    @Override
    public List<UserCountByLastname> countUsersByLastname(String surname) {
        return usersMapper.countUsersByLastname(surname);
    }

}
